﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.TypeConvert;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CatUtils.Utils.Enhance.Encrypt
{
    /// <summary>
    /// 弱类型，反转byte，对HTML加密效果差，效率极高，但是Byte内存占用无法回收，原因不明。
    /// </summary>
    public class ByteEncrypt
    {
        /// <summary>
        /// 加密string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string EnstringEncrypt(string str)
        {
            try
            {
                byte[] vs = new ByteConvert().StrTobyte(str);
                Array.Reverse(vs);
                return Encoding.UTF8.GetString(vs);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 解密string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string DestringEncrypt(string str)
        {
            return EnstringEncrypt(str);
        }

        /// <summary>
        /// 解码文件加密
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public string DeFileEncrypt(string FileName)
        {
            try
            {
                byte[] vs = File.ReadAllBytes(FileName);
                Array.Reverse(vs);
                return Encoding.UTF8.GetString(vs);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return "";
        }

        /// <summary>
        /// 批量加密文件
        /// </summary>
        /// <param name="FloderPath"></param>
        public void EnFileEncryptBatch(string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = FloderNameHelper.GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                EnFileEncrypt(SingleFile);
            }
        }

        /// <summary>
        ///加密文件
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NewFileName"></param>
        public string EnFileEncrypt(string FileName, string NewFileName)
        {
            try
            {
                byte[] vs = File.ReadAllBytes(FileName);
                Array.Reverse(vs);
                File.WriteAllBytes(NewFileName, vs);
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return NewFileName;
        }

        /// <summary>
        /// 加密文件
        /// </summary>
        /// <param name="FileName"></param>
        public string EnFileEncrypt(string FileName)
        {
            return EnFileEncrypt(FileName, FileNameHelper.CreateFileNameWithChildFloder(FileName, "反转"));
        }
    }
}